﻿/*
gritter提示方法封装，依赖jquery.js、gritter.min.js、gritter.min.css
type：1 success、2 error、3 info、4 warning
text：提示内容
title：提示标题
time：单位毫秒，默认5000毫秒
*/
function msg_tips(type, text, title, time) {
    var typefilter = '', titlestr = '';
    switch (type) {
        case 1:
        default:
            titlestr = '成功';
            typefilter = 'gritter-success';
            break;
        case 2:
            titlestr = '失败';
            typefilter = 'gritter-error';
            break;
        case 3:
            titlestr = '提示';
            typefilter = 'gritter-info';
            break;
        case 4:
            titlestr = '警告';
            typefilter = 'gritter-warning';
            break;
    }
    $.gritter.add({
        title: title == undefined ? titlestr : title,
        text: text == undefined ? "操作成功" : text,
        time: time == undefined ? 5000 : time,
        class_name: typefilter + ' gritter-light',
        before_open: function () {
            if ($('.gritter-item-wrapper').length >= 3) {
                $.gritter.removeAll();
                return false;
            }
        },
    });
}

/*
toastr提示方法封装，依赖jquery.js、toastr.js、toastr.css
type：1 success（默认）、2 error、3 info、4 warning
text：提示内容（默认：操作成功）
position：1 右上、2 右下、3 左下、4 左上、5 上中（默认）、6 下中
title：提示标题
time：提示时间，单位毫秒（默认：5000）
*/
function showToastr(type, text, position, title, time) {
    var typeStr = '', positionStr = '';
    switch (type) {
        case 1:
        default:
            typeStr = "success";
            break;
        case 2:
            typeStr = "error";
            break;
        case 3:
            typeStr = "info";
            break;
        case 4:
            typeStr = "warning";
            break;
    }
    switch (position) {
        case 1:
            positionStr = "toast-top-right";
            break;
        case 2:
            positionStr = "toast-bottom-right";
            break;
        case 3:
            positionStr = "toast-bottom-left";
            break;
        case 4:
            positionStr = "toast-top-left";
            break;
        case 5:
        default:
            positionStr = "toast-top-center";
            break;
        case 6:
            positionStr = "toast-bottom-center";
            break;
    }
    toastr.options = {
        "positionClass": positionStr,
        "timeOut": time == undefined ? "5000" : time,
    }
    toastr[typeStr](text == undefined ? "操作成功" : text, title);
}

/*
bootbox弹框方法封装，依赖jquery、bootbox.min.js
type：1 alert、2 confirm、3 prompt
title：标题
*/
function msg_alert(type, title, callback) {
    bootbox.setDefaults({
        locale: "zh_CN"
    });

    if (type == 1) {
        bootbox.alert({
            size: 'small',
            message: title
        })
    }
    else if (type == 2) {
        bootbox.confirm({
            size: 'small',
            message: title,
            callback: function (result) {
                if (typeof callback == "function") {
                    callback(result);
                }
            }
        })
    }
    else if (type == 3) {
        bootbox.prompt({
            size: 'small',
            title: title,
            callback: function (result) {
                if (typeof callback == "function") {
                    callback(result);
                }
            }
        })
    }
    else {
        return;
    }
}

/*
获取url参数 用法：getQuerystring("param")
*/
function getQuerystring(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}

/*
格式化日期成：yyyy/MM/dd格式
*/
function getFormatDate(str) {
    var day = new Date(str);
    var Year = 0;
    var Month = 0;
    var Day = 0;
    var dateformat = "";
    //Year= day.getYear();//有火狐下2008年显示108的bug
    Year = day.getFullYear();//ie火狐下都可以
    Month = day.getMonth() + 1;
    Day = day.getDate();
    dateformat += Year + "/";
    if (Month >= 10) {
        dateformat += Month + "/";
    }
    else {
        dateformat += "0" + Month + "/";
    }
    if (Day >= 10) {
        dateformat += Day;
    }
    else {
        dateformat += "0" + Day;
    }
    return dateformat;
}

/*
jqGrid相关 修正翻页图标等
*/
function updatePagerIcons(table) {
    var replacement = {
        'ui-icon-seek-first': 'icon-double-angle-left bigger-140',
        'ui-icon-seek-prev': 'icon-angle-left bigger-140',
        'ui-icon-seek-next': 'icon-angle-right bigger-140',
        'ui-icon-seek-end': 'icon-double-angle-right bigger-140'
    };
    $('.ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon').each(function () {
        var icon = $(this);
        var $class = $.trim(icon.attr('class').replace('ui-icon', ''));
        if ($class in replacement) icon.attr('class', 'ui-icon ' + replacement[$class]);
    })
}

function enableTooltips(table) {
    $('.navtable .ui-pg-button').tooltip({ container: 'body' });
    $(table).find('.ui-pg-div').tooltip({ container: 'body' });
}

/*
jqGrid相关 格式化显示true和false
*/
function frt_true_false(cellvalue) {
    var val = '';
    switch (cellvalue) {
        case true:
            val = '是';
            break;
        case false:
            val = '否';
            break;
        default:
            val = '未知';
    }
    return val;
}
/*
jqGrid相关 格式化显示启用禁用 int（default、danger、info、primary、success、warning）
*/
function frt_enable_disable_int(cellvalue) {
    var val = '';
    switch (cellvalue) {
        case 0:
            val = '<span class="label label-danger">禁用</span>';
            break;
        case 1:
            val = '<span class="label label-success">启用</span>';
            break;
        case 2:
            val = '<span class="label label-warning">未审核</span>';
            break;
        default:
            val = '<span class="label label-default">未知</span>';
    }
    return val;
}

/*
jqGrid相关 格式化显示启用禁用 bool（default、danger、info、primary、success、warning）
*/
function frt_enable_disable_bool(cellvalue) {
    var val = '';
    switch (cellvalue) {
        case false:
            val = '<span class="label label-danger">禁用</span>';
            break;
        case true:
            val = '<span class="label label-success">启用</span>';
            break;
        default:
            val = '<span class="label label-default">未知</span>';
    }
    return val;
}

//获取随机的唯一id，随机不重复，长度固定
var getUID = function (len) {
    len = len || 10;
    len = parseInt(len, 10);
    len = isNaN(len) ? 10 : len;
    var seed = '0123456789abcdefghijklmnopqrstubwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ';
    var seedLen = seed.length - 1;
    var uid = '';
    while (len--) {
        uid += seed[Math.round(Math.random() * seedLen)]
    }
    return uid;
}
